package com.huazai.yangge.future;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author 华仔
 * @date 2024/2/21
 */
public class FutureDemo {
    //多线程|有返回|异步任务
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        final FutureTask<String> stringFutureTask = new FutureTask<>(new MyTask());

        Thread thread = new Thread(stringFutureTask);
        thread.start();

        final String res = stringFutureTask.get();
        System.out.println("res = " + res);
    }
}

class MyTask implements Callable<String>{
    @Override
    public String call() throws Exception {
        return "my callable invoke";
    }
}

// 优点 future+线程池执行多任务 显著提升熟读
// 缺点  get 容易导致阻塞 一单调用就必须等待执行完成 建议放到最后
// 可以轮询  但是轮询耗费cpu资源
